﻿package cn.edu.zjusc.Webservice;
import java.util.List;

import javax.ejb.Stateless;
import javax.interceptor.Interceptors;
import javax.jws.*;
import javax.jws.soap.SOAPBinding;
import javax.persistence.*;



import org.jboss.wsf.spi.annotation.WebContext;

import cn.edu.zjusc.POJO.*;
import cn.edu.zjusc.Security.ProfileInterceptor;
import cn.edu.zjusc.Security.UserCave;
@Stateless
@WebService( name = "RoomInfoService", serviceName = "RoomInfoService")   
@SOAPBinding(style = SOAPBinding.Style.RPC)
@WebContext (contextRoot="/ZJUSCIX/Service", urlPattern="/RoomInfoService") 
public class RoomInfoService {
	/**
	 * 获取所有考试
	 * @param strName
	 * @return
	 *JQuery发送样式 {
	 	<env:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://Webservice.zjusc.edu.cn/">
   		<env:Body>
      	<web:GetAllExamInfo>
         <person>HEHE</person>
      		</web:GetAllExamInfo>
   		</env:Body>
		</env:Envelope>
	 * }
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public List<RoomInfo> GetAllRoomInfo() {

		EntityManagerFactory emf =Persistence.createEntityManagerFactory("zjusc");
		EntityManager em=emf.createEntityManager();

		Query query=em.createQuery("from RoomInfo e");
		List roomList=query.getResultList();
		
		em.close();
		emf.close();
		return roomList;
	}
	//@Interceptors(Profile.class)        @WebParam(name = "uc") UserCave uc,
	public List<RoomInfo> GetRoomInfoByExamInfo(@WebParam(name="examPlace")String examPlace,
			@WebParam(name="studentType")String studentType,
			@WebParam(name="major")int major,
			@WebParam(name="faceOrPen")int faceOrPen
			
			) {

		EntityManagerFactory emf =Persistence.createEntityManagerFactory("zjusc");
		EntityManager em=emf.createEntityManager();

		Query query=em.createQuery("from RoomInfo e where e.ro_stuType='"+studentType+"' and e.ro_examPlace='"+
				examPlace+"' and e.ro_stuMajor='"+major+"'"+" and e.ro_faceOrPen='"+faceOrPen+"'");
		List stuExtendBkList=query.getResultList();
		
		em.close();
		emf.close();
		return stuExtendBkList;
	}
	
	/**
	 * 新增一条考试
	 * @param eiBean
	 * @return
	 * JQuery发送样式{
		<env:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://Webservice.zjusc.edu.cn/">
		   <env:Body>
		      <web:NewExamInfo>
		         <examEntry>
		            <classRoomName>324</classRoomName>
		            <duration>30</duration>
		            <id></id>
		            <note>呵呵</note>
		            <roomId>120</roomId>
		            <startTime>2010-11-11T12:00:00</startTime>
		            <subject>1</subject>
		         </examEntry>
		      </web:NewExamInfo>
		   </env:Body>
		</env:Envelope>
		}
	 */
	@Interceptors(ProfileInterceptor.class)
	public String NewRoomInfo(@WebParam(name = "uc") UserCave uc,
			@WebParam(name = "roomEntry") RoomInfo eiBean) {
		EntityManagerFactory emf =Persistence.createEntityManagerFactory("zjusc");
		EntityManager em=emf.createEntityManager();
		
		//开始事务
		em.getTransaction().begin();
		em.persist(eiBean);
		//提交事务
		em.getTransaction().commit();
		
		em.close();
		emf.close();
		
		return "新增考场条目成功";
	}
	
	/**
	 * 修改一条考试
	 * @param eiBean
	 * @return
	 * JQuery发送格式
	 * 数据库中存在ID为7的数据
	 * {
			 * <env:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://Webservice.zjusc.edu.cn/">
		   <env:Body>
		      <web:ModifyExamInfo>
		         <examEntry>
		            <classRoomName>325</classRoomName>
		            <duration>50</duration>
		            <id>7</id>
		            <note>呵呵</note>
		            <roomId>121</roomId>
		            <startTime>2012-02-02T12:00:00</startTime>
		            <subject>2</subject>
		         </examEntry>
		      </web:ModifyExamInfo>
		   </env:Body>
		</env:Envelope>
	 * }
	 */
	@Interceptors(ProfileInterceptor.class)
	public String ModifyRoomInfo(@WebParam(name = "uc") UserCave uc,
			@WebParam(name = "roomEntry") RoomInfo eiBean) {
		EntityManagerFactory emf =Persistence.createEntityManagerFactory("zjusc");
		EntityManager em=emf.createEntityManager();
		
		//开始事务
		em.getTransaction().begin();

		em.merge(eiBean);
		//提交事务
		em.getTransaction().commit();
		
		em.close();
		emf.close();
		
		return "修改考场条目成功";
	}
	
	/**
	 * 删除一条考试
	 * @param examID
	 * @return
	 * JQuery发送格式{
		<env:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://Webservice.zjusc.edu.cn/">
		   <env:Body>
		      <web:DeleteExamInfo>
		         <id>8</id>
		      </web:DeleteExamInfo>
		   </env:Body>
		</env:Envelope>
	 * }
	 */
	@Interceptors(ProfileInterceptor.class)
	public String DeleteRoomInfo(@WebParam(name = "uc") UserCave uc,
			@WebParam(name = "id") String roomID) {
		EntityManagerFactory emf =Persistence.createEntityManagerFactory("zjusc");
		EntityManager em=emf.createEntityManager();
		
		//开始事务
		em.getTransaction().begin();
		RoomInfo eiBean=em.find(RoomInfo.class, roomID);
		em.remove(eiBean);
		//提交事务
		em.getTransaction().commit();
		
		em.close();
		emf.close();
		
		return "删除考场条目成功";
	}
}
